From theseas!fs.Princeton.EDU!cek Sat, 6 Feb 93 01:37:22 EET
Received: by kriton.UUCP (V1.16/Amiga)
	id AA00000; Sat, 6 Feb 93 01:37:22 EET
Received: by theseas.ntua.gr with UUCP; Wed, 3 Feb 93 02:32:14 +0200
Received: from mcsun.EU.net by pythia.ics.forth.gr via ITEnet with SMTP;
	id AA00647 (5.65c/FORTH-ICS-3.0-MHS-7.0); Wed, 3 Feb 1993 02:26:47 +0200
Received: by mcsun.EU.net via EUnet
	id AA27047 (5.65b/CWI-2.205); Wed, 3 Feb 1993 01:24:36 +0100
Received: from Princeton.EDU by relay2.UU.NET with SMTP 
	(5.61/UUNET-internet-primary) id AA23214; Tue, 2 Feb 93 19:22:40 -0500
Received: from fs.Princeton.EDU by Princeton.EDU (5.65b/2.95/princeton)
	id AA03959; Tue, 2 Feb 93 18:18:11 -0500
Received: by fs.Princeton.EDU (4.1/1.105)
	id AA21112; Tue, 2 Feb 93 18:18:07 EST
Received: from fsa.cpsc.ucalgary.ca by fs.Princeton.EDU (4.1/1.105)
	id AA21011; Tue, 2 Feb 93 18:17:05 EST
Received: from do.cpsc.ucalgary.ca by fsa.cpsc.ucalgary.ca (4.1/CSd1.2)
	id AA01083; Tue, 2 Feb 93 15:23:03 MST
Received: by do.cpsc.ucalgary.ca (4.1/cs1.0)
	id AA23391; Tue, 2 Feb 93 15:23:07 MST
Date: Tue, 2 Feb 93 15:23:03 MST
Message-Id: <9302022223.AA01083@fsa.cpsc.ucalgary.ca>
Errors-To: Princeton.EDU!cek
Remailed-Date: Tue Feb  2 18:17:30 EST 1993
From: cpsc.ucalgary.ca!jamesm (Mark James)
To: cs.Princeton.EDU!rayshade-users
Subject: Gnu mmalloc and rayshade

A few of the people in our department are raytracing some fairly
large scenes with rayshade.  For example, a friend of mine has
modeled a field of grass which gives him an 80MB input file. :)  One
problem with this is that rayshade fairly quickly runs out of memory.
:)  However, there is a way around this.  GDB 4.7 has a library
called mmalloc which uses mmap for allocating memory rather than
sbrk.  So now all you need to do is specify the name of a swap file
on a big disk, and rayshade can chew up all the memory it needs.
It's not wonderfully fast, but what can you expect with an 80MB input
file, and at least it gets the job done. :)

I've patched rayshade.4.0.6 to use mmalloc.  The patch isn't pretty,
and it doesn't change the Configure script which it really should,
but here it is anyhow.  When Configure asks for C flags, add
"-DMMALLOC", and specify the libmmalloc library when it asks you
about libraries. Please let me know if you have problems with it, or
find it useful.

     M.

use: patch -p0 -d rayshade.4.0 < mmalloc-patch

*** ./libray/libcommon/expr.c	Sun Feb  9 20:04:04 1992
--- ../mrayshade/./libray/libcommon/expr.c	Tue Feb  2 10:47:10 1993
***************
*** 289,295 ****
  {
  	if (!expr->symtab) {
  		if (expr->type == BUILTIN_EXPR && expr->params)
! 			free((voidstar)expr->params);
! 		free((voidstar)expr);
  	}
  }
--- 289,295 ----
  {
  	if (!expr->symtab) {
  		if (expr->type == BUILTIN_EXPR && expr->params)
! 			Free((voidstar)expr->params);
! 		Free((voidstar)expr);
  	}
  }
*** ./libray/libcommon/memory.c	Sun Feb  9 20:03:29 1992
--- ../mrayshade/./libray/libcommon/memory.c	Tue Feb  2 10:47:10 1993
***************
*** 24,31 ****
--- 24,33 ----
  #include <memory.h>
  #endif
  #include "common.h"
+ #include "../../libshade/options.h"
  
  unsigned long TotalAllocated;
+ unsigned long TotalDeallocated;
  
  voidstar
  Malloc(bytes)
***************
*** 33,41 ****
  {
  	voidstar res;
  
! 	TotalAllocated += bytes;
! 
  	res = (voidstar)malloc(bytes);
  	if (res == (voidstar)NULL)
  		RLerror(RL_PANIC,
  			"Out of memory trying to allocate %d bytes.\n",bytes);
--- 35,45 ----
  {
  	voidstar res;
  
! #ifdef MMALLOC
! 	res = (voidstar)mmalloc(Options.mmallocd,bytes);
! #else
  	res = (voidstar)malloc(bytes);
+ #endif
  	if (res == (voidstar)NULL)
  		RLerror(RL_PANIC,
  			"Out of memory trying to allocate %d bytes.\n",bytes);
***************
*** 51,56 ****
--- 55,71 ----
  	res = Malloc(nelem*elen);
  	bzero(res, (int)nelem*elen);
  	return res;
+ }
+ 
+ void
+ Free(pointer)
+ voidstar pointer;
+ {
+ #ifdef MMALLOC
+         mfree(Options.mmallocd,pointer);
+ #else
+ 	free(pointer);
+ #endif
  }
  
  void
*** ./libray/libcommon/transform.c	Sun Feb  9 20:04:12 1992
--- ../mrayshade/./libray/libcommon/transform.c	Tue Feb  2 10:47:12 1993
***************
*** 57,64 ****
  Trans *trans;
  {
  	if (trans->tr)
! 		free((voidstar)trans->tr);
! 	free((voidstar)trans);
  }
  
  void
--- 57,64 ----
  Trans *trans;
  {
  	if (trans->tr)
! 		Free((voidstar)trans->tr);
! 	Free((voidstar)trans);
  }
  
  void
*** ./libray/libobj/blob.c	Sun Feb  9 20:04:19 1992
--- ../mrayshade/./libray/libobj/blob.c	Tue Feb  2 10:47:21 1993
***************
*** 131,137 ****
  		blob->list[i].y = cur->mvec.y;
  		blob->list[i].z = cur->mvec.z;
  		mlist = mlist->next;
! 		free((voidstar)cur);
  	}
  	/* 
  	 * Allocate room for Intersection Structures and
--- 131,137 ----
  		blob->list[i].y = cur->mvec.y;
  		blob->list[i].z = cur->mvec.z;
  		mlist = mlist->next;
! 		Free((voidstar)cur);
  	}
  	/* 
  	 * Allocate room for Intersection Structures and
*** ./libray/libobj/geom.c	Sun Feb  9 20:04:11 1992
--- ../mrayshade/./libray/libobj/geom.c	Tue Feb  2 10:47:22 1993
***************
*** 306,312 ****
  	GeomList *ltmp;
  
  	ltmp = list->next;	/* Save new head. */
! 	free((voidstar)list);	/* Free old head. */
  	return ltmp;		/* Return new head. */
  }
  
--- 306,312 ----
  	GeomList *ltmp;
  
  	ltmp = list->next;	/* Save new head. */
! 	Free((voidstar)list);	/* Free old head. */
  	return ltmp;		/* Return new head. */
  }
  
*** ./libray/libobj/grid.c	Sun Feb  9 20:04:13 1992
--- ../mrayshade/./libray/libobj/grid.c	Tue Feb  2 10:47:22 1993
***************
*** 396,402 ****
  			for (z = 0; z < grid->zsize; z++) {
  				for (cell = grid->cells[x][y][z]; cell; cell = next) {
  					next = cell->next;
! 					free((voidstar)cell);
  				}
  				grid->cells[x][y][z] = (GeomList *)NULL;
  			}
--- 396,402 ----
  			for (z = 0; z < grid->zsize; z++) {
  				for (cell = grid->cells[x][y][z]; cell; cell = next) {
  					next = cell->next;
! 					Free((voidstar)cell);
  				}
  				grid->cells[x][y][z] = (GeomList *)NULL;
  			}
*** ./libray/libobj/hf.c	Sun Feb  9 20:04:18 1992
--- ../mrayshade/./libray/libobj/hf.c	Tue Feb  2 10:47:22 1993
***************
*** 210,217 ****
  		hitpos.x = ray->pos.x + ray->dir.x * offset;
  		hitpos.y = ray->pos.y + ray->dir.y * offset;
  		hitpos.z = ray->pos.z + ray->dir.z * offset;
! 	} else
! 		hitpos = ray->pos;
  	/*
  	 * Find out in which cell "hitpoint" is.
  	 */
--- 210,216 ----
  		hitpos.x = ray->pos.x + ray->dir.x * offset;
  		hitpos.y = ray->pos.y + ray->dir.y * offset;
  		hitpos.z = ray->pos.z + ray->dir.z * offset;
! 	}
  	/*
  	 * Find out in which cell "hitpoint" is.
  	 */
***************
*** 509,514 ****
--- 508,515 ----
  	tmp2.y = tri->v3.y - tri->v1.y;
  	tmp2.z = tri->v3.z - tri->v1.z;
  
+         VecScale(hf->size, tmp1, &tmp1);
+         VecScale(hf->size, tmp2, &tmp2);
  	(void)VecNormCross(&tmp1, &tmp2, &tri->norm);
  
  	tri->d = -dotp(&tri->v1, &tri->norm);
***************
*** 657,663 ****
  hfTri *tri;
  {
  	if (hf->q[hf->qtail])		/* Free old triangle data */
! 		free((voidstar)hf->q[hf->qtail]);
  	hf->q[hf->qtail] = tri;		/* Put on tail */
  	hf->qtail = (hf->qtail + 1) % hf->qsize;	/* Increment tail */
  }
--- 658,664 ----
  hfTri *tri;
  {
  	if (hf->q[hf->qtail])		/* Free old triangle data */
! 		Free((voidstar)hf->q[hf->qtail]);
  	hf->q[hf->qtail] = tri;		/* Put on tail */
  	hf->qtail = (hf->qtail + 1) % hf->qsize;	/* Increment tail */
  }
*** ./libray/libobj/poly.c	Sun Feb  9 20:04:08 1992
--- ../mrayshade/./libray/libobj/poly.c	Tue Feb  2 10:47:22 1993
***************
*** 67,73 ****
  	for(curp = plist; curp != (PointList *)0; curp = pltmp) {
  		poly->points[i--] = curp->vec;
  		pltmp = curp->next;
! 		free((voidstar)curp);
  	}
  
  	/*
--- 67,73 ----
  	for(curp = plist; curp != (PointList *)0; curp = pltmp) {
  		poly->points[i--] = curp->vec;
  		pltmp = curp->next;
! 		Free((voidstar)curp);
  	}
  
  	/*
***************
*** 86,92 ****
  		 * Degenerate normal --> degenerate polygon
  		 */
  		RLerror(RL_WARN, "Degenerate polygon.\n");
! 		free((voidstar)poly->points);
  		return (Polygon *)NULL;
  	}
  
--- 86,92 ----
  		 * Degenerate normal --> degenerate polygon
  		 */
  		RLerror(RL_WARN, "Degenerate polygon.\n");
! 		Free((voidstar)poly->points);
  		return (Polygon *)NULL;
  	}
  
*** ./libray/libsurf/surface.c	Sun Feb  9 20:03:52 1992
--- ../mrayshade/./libray/libsurf/surface.c	Tue Feb  2 10:47:26 1993
***************
*** 135,141 ****
  {
  	SurfList *stmp = list->next;
  
! 	free((voidstar)list);
  	return stmp;
  }
  
--- 135,141 ----
  {
  	SurfList *stmp = list->next;
  
! 	Free((voidstar)list);
  	return stmp;
  }
  
*** ./libshade/funcdefs.h	Sun Feb  9 20:03:18 1992
--- ../mrayshade/./libshade/funcdefs.h	Tue Feb  2 10:47:33 1993
***************
*** 36,42 ****
   * Misc. routines.
   */
  #ifndef I_STDLIB
! extern void	free(), exit();
  #endif
  
  extern void get_cpu_time(), read_input_file();
--- 36,42 ----
   * Misc. routines.
   */
  #ifndef I_STDLIB
! extern void	Free(), exit();
  #endif
  
  extern void get_cpu_time(), read_input_file();
*** ./libshade/misc.c	Sun Feb  9 20:03:59 1992
--- ../mrayshade/./libshade/misc.c	Tue Feb  2 10:47:33 1993
***************
*** 37,42 ****
--- 37,46 ----
  #include "options.h"
  #include "stats.h"
  
+ #ifdef MMALLOC
+ #include <fcntl.h>
+ #endif
+ 
  Float RSabstmp;	/* Temporary value used by fabs macro.  Ugly. */
  static void RSmessage();
  
***************
*** 103,108 ****
--- 107,135 ----
  			"Cannot open stats file %s.\n", Options.statsname);
  	}
  }
+ 
+ #ifdef MMALLOC
+ void
+ InitMMalloc()
+ {
+ 	int fd;
+ 	if (Options.mmapname == (char *)NULL)
+ 		return;		/* Not specified or already opened. */
+ 
+ 	fd = open(Options.mmapname, (O_CREAT | O_RDWR), 0600);
+ 	unlink(Options.mmapname);
+ 	if (fd < 0) {
+ 		RLerror(RL_PANIC,
+ 			"Cannot open mmalloc file %s.\n", Options.statsname);
+ 	}
+ 	Options.mmallocd=(voidstar)mmalloc_attach(fd,0xE0000000);  /* 0xE... is a hack from GDB 4.7  */
+ 	if(Options.mmallocd==0){
+ 		RLerror(RL_PANIC,
+ 			"Mmalloc_attatch failed!\n");
+ 	}
+ 	
+ }
+ #endif
  
  void
  RLerror(level, pat, arg1, arg2, arg3)
*** ./libshade/options.c	Sun Feb  9 20:04:09 1992
--- ../mrayshade/./libshade/options.c	Tue Feb  2 10:47:33 1993
***************
*** 137,142 ****
--- 137,157 ----
  				Options.samplemap = !Options.samplemap;
  				break;
  #endif
+ #ifdef MMALLOC
+ 			case 'M':
+ 				if (argv[1][0] == '-') {
+ 					/* User probably blew it, and
+ 					 * it's difficult to remove a file
+ 					 * that begins with '-'...
+ 					 */
+ 					usage();
+ 					exit(2);
+ 				}
+ 				Options.mmapname = strsave(argv[1]);
+ 				InitMMalloc();
+ 				argv++; argc--;
+ 				break;
+ #endif				
  			case 'N':
  				Options.totalframes = atof(argv[1]);
  				Options.totalframes_set = TRUE;
***************
*** 317,322 ****
--- 332,340 ----
  	fprintf(stderr,"\t-l \t\t(Render image for left eye view.)\n");
  #ifdef URT
  	fprintf(stderr,"\t-m \t\t(Output sample map in alpha channel.)\n");
+ #endif
+ #ifdef MMALLOC
+ 	fprintf(stderr,"\t-M filename \t(Use filename for swapping.)\n");
  #endif
  	fprintf(stderr,"\t-N number\t(Render given number of frames.)\n");
  	fprintf(stderr,"\t-n \t\t(Do not render shadows.)\n");
*** ./libshade/options.h	Sun Feb  9 20:03:53 1992
--- ../mrayshade/./libshade/options.h	Tue Feb  2 10:47:33 1993
***************
*** 74,79 ****
--- 74,83 ----
  	int	alpha;			/* Write alpha channel? */
  	int	exp_output;		/* Write exponential RLE file? */
  #endif
+ #ifdef MMALLOC
+ 	char * mmapname;
+ 	void * mmallocd;
+ #endif
  	Float	eyesep,			/* Eye separation (for Stereo mode) */
  		gamma,			/* Gamma value (0 == no correction) */
  		starttime,		/* Think about it ... */
*** ./libshade/shade.c	Sun Feb  9 20:04:12 1992
--- ../mrayshade/./libshade/shade.c	Tue Feb  2 10:47:34 1993
***************
*** 343,349 ****
  		ColorAdd(*color, newcol, color);
  		/* Free pushed medium */
  		if (enter)
! 			free((voidstar)NewRay.media);
  	}
  
  	return total_int_refl;
--- 343,349 ----
  		ColorAdd(*color, newcol, color);
  		/* Free pushed medium */
  		if (enter)
! 			Free((voidstar)NewRay.media);
  	}
  
  	return total_int_refl;
*** ./libshade/yacc.y	Sun Feb  9 20:04:21 1992
--- ../mrayshade/./libshade/yacc.y	Tue Feb  2 10:47:35 1993
***************
*** 537,543 ****
  			 * Eye can be transformed...
  			if (CurMatrix) {
  				PointTransform(&Camera.pos, CurMatrix);
! 				free((voidstar)CurMatrix);
  				CurMatrix = (Matrix*)NULL;
  			}
  			 */
--- 537,543 ----
  			 * Eye can be transformed...
  			if (CurMatrix) {
  				PointTransform(&Camera.pos, CurMatrix);
! 				Free((voidstar)CurMatrix);
  				CurMatrix = (Matrix*)NULL;
  			}
  			 */
***************
*** 806,812 ****
  			 * converted from 3.0, surfnames this can account
  			 * for lots o' bytes.
  			 */
! 			free((voidstar)$1);
  		}
  		| tCURSURF
  		{
--- 806,812 ----
  			 * converted from 3.0, surfnames this can account
  			 * for lots o' bytes.
  			 */
! 			Free((voidstar)$1);
  		}
  		| tCURSURF
  		{
*** ./raypaint/render.c	Sun Feb  9 20:04:20 1992
--- ../mrayshade/./raypaint/render.c	Tue Feb  2 10:47:40 1993
***************
*** 232,238 ****
  		PictureWriteLine(pixelbuf);
  	}
  	PictureEnd();
! 	free((voidstar)pixelbuf);
  }
  
  Float
--- 232,238 ----
  		PictureWriteLine(pixelbuf);
  	}
  	PictureEnd();
! 	Free((voidstar)pixelbuf);
  }
  
  Float
*** ./rayview/spheregen.c	Sun Feb  9 20:04:07 1992
--- ../mrayshade/./rayview/spheregen.c	Tue Feb  2 10:47:42 1993
***************
*** 204,211 ****
  	}
  
  	if (level > 1) {
! 	    free(old->poly);
! 	    free(old);
  	}
  
  	/* Continue subdividing new triangles */
--- 204,211 ----
  	}
  
  	if (level > 1) {
! 	    Free(old->poly);
! 	    Free(old);
  	}
  
  	/* Continue subdividing new triangles */

----------
Administrivia: rayshade-request@cs.princeton.edu
Mailing list: rayshade-users@cs.princeton.edu

